﻿
// Condition: 13. Write a program that can solve these tasks:
//                - reverse the digits of a number
//                - Calculates the average of a number
//                - Solve a linear equation a*x+b=0
// Create appropriate methods. Provide a simple text-based menu for the user to choose which task to solve.
// Validate the input data:
// - The decimal number should be non-negative
// - The sequence should not be empty
// - a should not be equal to 0 

using System;



class ManyTasks
{   
    // reverse method
    static void Reverse()
    {
        // input info and validate it
        Console.Write("Enter decimal number: ");
        decimal num = decimal.Parse(Console.ReadLine());
        while (num<0)
        {
            Console.WriteLine("Decimal number should be no-negative");
            Console.Write("Enter decimal: ");
            num = decimal.Parse(Console.ReadLine());
        }

        // solution convert decimal to string and make another string with reverse digits
        string strNum = Convert.ToString(num);
        string digits = null;
        for (int i = strNum.Length-1; i >= 0; i--)
        {
            if (strNum[i] == ',' || strNum[i] == '.')
            {
                continue;
            }
            else
            {
                digits += strNum[i];
            }
        }
        // print solution
        Console.WriteLine("The reverse digits of number {0} is: {1}",num,digits);
    }

    // average method
    static void Average()
    {
        // input info and validate it
        bool check = true;
        bool warning = true;
        Console.Write("Enter number of elements in your sequence: ");
        int element = int.Parse(Console.ReadLine());
        int[] array = new int[element];

        while (check)
        {
            if (warning == false)
            {
                Console.WriteLine("Your sequence of integers is empty, try again.");
            }

            for (int i = 0; i < array.Length; i++)
            {
                Console.Write("Element[{0}]= ");
                array[i] = int.Parse(Console.ReadLine());
            }
            foreach (var el in array)
            {
                if (el == 0)
                {
                    continue;
                }
                else
                {
                    check = false;
                }
            }
            warning = false;

        }

        // sum of all element and find average
        double average;
        int sum = 0;
        foreach (var el in array)
        {
            sum += el;
        }
        average = ((double)sum / (double)array.Length);

        // print output
        Console.Write("The average of sequence: { ");
        foreach (var el in array)
        {
            Console.Write(el + " ");
        }
        Console.Write("} is: ");
        Console.Write("{0:f2}", average);
        Console.WriteLine();
    }

    static void LinearEquation()
    {
        // enter coefficients and validate it
        Console.Write("Enter first coefficient a= ");
        double a = double.Parse(Console.ReadLine());
        while (a==0)
        {
            Console.WriteLine("Coefficient a should be different than 0.");
            Console.Write("Enter first coefficient a= ");
            a = double.Parse(Console.ReadLine());
        }

        // find x and print result
        Console.Write("Enter second coefficient b= ");
        double b = double.Parse(Console.ReadLine());
        double x = -(b/a);
        Console.WriteLine("For equation a*x+b=0 where a={0} and b={1}, than x={2:f2}",a,b,x);


    }

    static void Main()
    {
        // create enter menu
        Console.WriteLine("Choose what task you want to solve?");
        Console.WriteLine("Menu:");
        Console.WriteLine("To reverse the digits of a decimal number - PRESS 1");
        Console.WriteLine("To calculate the average of a sequence of integers = PRESS 2");
        Console.WriteLine("To solve linear equation - PRESS 3");
        int choise = int.Parse(Console.ReadLine());

        // method according to your choice
        if (choise == 1)
        {
            Console.WriteLine();
            Reverse();
        }
        if (choise == 2)
        {
            Console.WriteLine();
            Average();
        }
        if (choise == 3)
        {
            Console.WriteLine();
            LinearEquation();
        }

    }
}

